<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$url     =    $_SC ['apiurl'];
$mpurl	 =    'admin.php?ac=count_topic';

if(isset($_GET['op']) && $_GET['op'] == 'getTopicInfo'){
    // 获取话题详情
    $topicInfo    =    getAdminTopicInfo($url, intval($_GET['tid']));
    
    // 初始化分页数据
    $start   			=   intval($_GET ['start']) > 0   ? intval($_GET ['start']) : 0;
    $perpage 			=   intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;
    if (isset($_GET['page'])){
        $page  			=   intval($_GET['page']);
        $start          =   ($page-1) * $perpage;
    }
    
    // 组装数据
    $order      =    'favorites';
    $sort       =    'DESC';
    $tid        =    intval($_GET['tid']);
    $author_uid =    intval($topicInfo['uid']);
    if (!empty($_GET['order']))    $order      =    strval($_GET['order']);
    if (!empty($_GET['sort']))     $sort       =    strval($_GET['sort']);
    
    // 获取话题评论
    $topicComments  =  getAdminTopicCommentList($url, $tid, $author_uid, $order, $sort, $start, $perpage);
    
    // 分页数据组装
    $total       =  $topicInfo['res_num'];
    $totalPages  = intval(ceil($total/$perpage));
    $currentPage =  intval($_GET['page']);
    if (empty($_GET['page'])) $currentPage = 1;

    $start      =   ($currentPage - 1) * $perpage;
    $mpurl	   .= 	"&op=getTopicInfo&tid=".$tid."&order=".$order."&sort=".$sort;
    $pageurl 	= 	multitotal($currentPage, $totalPages, $mpurl);
    $nextpurl 	= 	$pageurl['next'];
    $prevpurl 	= 	$pageurl['prev'];
    $mpurl 		= 	$pageurl['urls'];
    $firstpurl  = 	$pageurl['first'];
    $lastpurl   = 	$pageurl['last'];
    $nowPage	=	$start / $perpage + 1;
    
    // 条件恢复
    $orderbyArr		=	array($order	 => " selected");
	$orderscArr 	=	array($sort      => " selected");
	$perpageArr 	=	array($perpage   => " selected");
    
    include_once template("admin/topic_info");
    exit();
}elseif ($_GET['op'] == 'analysisTCUsers'){
    $tid    = 0;
    $result = array();
    if ($_GET['tid']) {
        $tid       =    intval($_GET['tid']);
        $result    =    analysisTCUsers($url, $tid);
    }

    $age  = !empty($result['age'])    ? $result['age']    : array();
    $area = !empty($result['area'])   ? $result['area']   : array();
    $tag  = !empty($result['tag'])    ? $result['tag']    : array();
    $count= !empty($result['counts']) ? $result['counts'] : 0;
    $data = !empty($result['data'])   ? $result['data']   : array();
    
    include_once template("admin/analysis_topic_user");
    exit();
}else{
    // 获取全部圈子
    $allGroups = getAllGroups($url);
    
    // 初始化默认时间
    $time1 = $datereply1 = strtotime(date("Y-m-d 00:00:00",strtotime("last days")));
    $time2 = $datereply2 = strtotime(date("Y-m-d 00:00:00",time()));
    
    // 初始化参数
    $param = array(
            'gid'        =>    1,
            'time1'      =>    $time1,
            'time2'      =>    $time2,
            'orderby'    =>    'tid',
            'ordersc'    =>    'desc',
            'last_reply1'=>    $datereply1,
            'last_reply2'=>    $datereply2
    );
    
    // 初始化分页数据
    $start   			=   intval($_GET ['start']) > 0   ? intval($_GET ['start']) : 0;
    $perpage 			=   intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;
    if (isset($_GET['page'])){
        $page  			=   intval($_GET['page']);
        $start          =   ($page-1) * $perpage;
    }
    
    // 页面传上来的参数
    if ($_GET['gid'])    $param          =   $_GET;
    isset($_GET['time1'])          &&    $param['time1']          =    $time1        =    strtotime($_GET['time1']);
    isset($_GET['time2'])          &&    $param['time2']          =    $time2        =    strtotime($_GET['time2']);
    isset($_GET['last_reply1'])    &&    $param['last_reply1']    =    $datereply1   =    strtotime($_GET['last_reply1']);
    isset($_GET['last_reply2'])    &&    $param['last_reply2']    =    $datereply2   =    strtotime($_GET['last_reply2']);
    
    // 组装请求参数
    $param['do']		=	'getAdminTopicList';
    $param['format']	=	'json';
    $param['start']		=	$start;
    $param['perpage']	=	$perpage;
    
    // 请求
	$topicList          =   requestAndResponse($_SC ['apiurl'], $param);

	// 处理返回结果
	$urls      			=  	$topicList['urls'];
	$total   			=  	$topicList['total'];
	$topicList 			=  	$topicList['topics'];
	$total = count($topicList) - 1;
	unset($topicList[$total]);
	
	// 分页数据组装
	$totalPages  = intval(ceil($total/$perpage));
	$currentPage =  intval($_GET['page']);
	if (empty($_GET['page'])) $currentPage = 1;

	$start      =   $start + $perpage;
	$mpurl	   .= 	$urls;
	$pageurl 	= 	multitotal($currentPage, $totalPages, $mpurl);
	$nextpurl 	= 	$pageurl['next'];
	$prevpurl 	= 	$pageurl['prev'];
	$mpurl 		= 	$pageurl['urls'];
	$firstpurl  = 	$pageurl['first'];
	$lastpurl   = 	$pageurl['last'];
	$nowPage	=	$start / $perpage + 1;
	
	// 恢复搜索条件
	$intkeys = array(
	        'response_num',
	        'fav_num',
	        'share_num',
	        'view_num',
	        'like_num',
	        'recom_num',
	        'last_reply',
	        'time'
	);
	
	// 恢复条件恢复
	$uid			=	$param['uid'];
	$orderbyArr		=	array($param['orderby']	 => " selected");
	$orderscArr 	=	array($param['ordersc']  => " selected");
	$perpageArr 	=	array($param['perpage']  => " selected");
	$gidArr 		=	array($param['gid']  	 => " selected");
	$respArr		=	array($param['response_num1'],  $param['response_num2']);
	$favArr			=	array($param['fav_num1'],		$param['fav_num2']);
	$shareArr		=	array($param['share_num1'],		$param['share_num2']);
	$viewArr		=	array($param['view_num1'],  $param['view_num2']);
	$likeArr		=	array($param['like_num1'], 	$param['like_num2']);
	$recomArr		=	array($param['recom_num1'], $param['recom_num2']);
	$timeArr		=	array($time1, $time2);
	$replyArr		=	array($datereply1, $datereply2);
}

// 获取全部圈子
function analysisTCUsers($url, $tid){
    $param = array(
        'do'          =>    'group',
        'action'      =>    'getTopicCommentUids',
        'tid'         =>    intval($tid),
        'format'      =>    'json'
    );

    $result 		=  	requestAndResponse($url, $param);
    return $result;
}

// 获取全部圈子
function getAllGroups($url){
    $param = array(
            'do'          =>    'getAllGroups',
            'format'      =>    'json'
    );
    
    $allGroups 		=  	requestAndResponse($url, $param);
    $result = $allGroups['groups'];
    return $result;
}

// 获取话题详情
function getAdminTopicInfo($url, $tid){
    $param = array(
            'do'          =>    'getAdminTopicInfo',
            'format'      =>    'json',
            'tid'         =>    $tid
    );
    
    $topicInfo 		=  	requestAndResponse($url, $param); 
    $result			=	$topicInfo['topic_info'];
    return $result;
}

// 获取话题评论列表
function getAdminTopicCommentList($url, $tid, $author_uid, $order, $sort, $start, $length){
    $param = array(
            'do'          =>    'getAdminTopicCommentList',
            'format'      =>    'json',
            'tid'         =>    $tid,
            'author_uid'  =>    $author_uid,
            'order'       =>    $order,
            'sort'        =>    $sort,
            'start'       =>    $start,
            'length'      =>    $length
    );

    $topicComment 	=  	requestAndResponse($url, $param);
    $result			=	$topicComment['comments'];

    return $result;
}
